home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / SETENVAR.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  4KB  |  147 lines

  1. /*
  2. **  SETENVAR.C - Program which sets the DOS master environment upon exit
  3. **
  4. **  by: Bob Stout
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <conio.h>
  11. #include <dos.h>
  12.  
  13. #if !defined(__ZTC__) && !defined(__TURBOC__)
  14.  #define MK_FP(seg,offset) \
  15.         ((void far *)(((unsigned long)(seg)<<16) | (unsigned)(offset)))
  16.  #define peek(s,o) (*((unsigned far *)(MK_FP(s,o))))
  17.  #define poke(s,o,w) (*((unsigned far *)(MK_FP(s,o)))=(w))
  18. #endif
  19.  
  20. #define SUCCESS 0
  21. #define ERROR -1
  22.  
  23. static unsigned head, tail;
  24. static int idx = 0;
  25. static unsigned keystack[16][2];
  26.  
  27. /*
  28. **  ungetkey()
  29. **
  30. **  Stuffs characters into the keyboard buffer.
  31. **
  32. **  Parameters: 1 - Extended character to stuff
  33. **
  34. **  Returns: SUCCESS or EOF
  35. **
  36. **  Note: This function assumes that the keyboard buffer is in
  37. **        the normal (for IBM) location of 40:1E.
  38. **
  39. */
  40.  
  41. int ungetkey(unsigned key)
  42. {
  43.         int count;
  44.  
  45. #ifdef __ZTC__
  46.         peek(0x40, 0x1a, &head, sizeof(unsigned));
  47.         peek(0x40, 0x1c, &tail, sizeof(unsigned));
  48. #else
  49.         head = peek(0x40, 0x1a);
  50.         tail = peek(0x40, 0x1c);
  51. #endif
  52.         count = tail - head;
  53.         if (0 > count)
  54.                 count += (16 * sizeof(unsigned));
  55.         count >>= 1;
  56.  
  57.         if (15 > count)
  58.         {
  59. #ifdef __ZTC__
  60.                 peek(0x40, tail, &keystack[idx][0], sizeof(unsigned));
  61. #else
  62.                 keystack[idx][0] = peek(0x40, tail);
  63. #endif
  64.                 keystack[idx][1] = tail;
  65. #ifdef __ZTC__
  66.                 poke(0x40, tail, &key, sizeof(unsigned));
  67. #else
  68.                 poke(0x40, tail, key);
  69. #endif
  70.                 tail += sizeof(unsigned);
  71.                 if (0x3e <= tail)
  72.                         tail = 0x1e;
  73. #ifdef __ZTC__
  74.                 poke(0x40, 0x1c, &tail, sizeof(unsigned));
  75. #else
  76.                 poke(0x40, 0x1c, tail);
  77. #endif
  78.                 return key;
  79.         }
  80.         return EOF;
  81. }
  82.  
  83. /*
  84. **  KB_stuff()
  85. **
  86. **  Stuffs strings into the keyboard buffer.
  87. **
  88. **  Parameters: 1 - String to stuff
  89. **
  90. **  Returns: SUCCESS if successful
  91. **           ERROR   in case of error, plus beyboard buffer is
  92. **                   restored
  93. **
  94. **  Note: This function assumes that the keyboard buffer is in
  95. **        the normal (for IBM) location of 40:1E.
  96. */
  97.  
  98. int KB_stuff(char *str)
  99. {
  100.         int ercode = SUCCESS;
  101.  
  102.         idx = 0;
  103.         while (*str)
  104.         {
  105.                 if (EOF == ungetkey((unsigned)(*str++)))
  106.                 {
  107.                         while (0 <= --idx)
  108.                         {
  109.                                 tail = keystack[idx][1];
  110. #ifdef __ZTC__
  111.                                 poke(0x40, tail, &keystack[idx][0],
  112.                                         sizeof(unsigned));
  113. #else
  114.                                 poke(0x40, tail, keystack[idx][0]);
  115. #endif
  116.                         }
  117. #ifdef __ZTC__
  118.                         poke(0x40, 0x1c, &tail, sizeof(unsigned));
  119. #else
  120.                         poke(0x40, 0x1c, tail);
  121. #endif
  122.                         ercode = ERROR;
  123.                         break;
  124.                 }
  125.                 else    ++idx;
  126.         }
  127.         idx = 0;
  128.         return ercode;
  129. }
  130.  
  131. void main(int argc, char *argv[])
  132. {
  133.         FILE *bfile;
  134.  
  135.         if (3 > argc)
  136.         {
  137.                 puts("\aUsage: SETENVAR envar datum");
  138.                 abort();
  139.         }
  140.         bfile = fopen("$TMP$.BAT", "w");
  141.         fprintf(bfile, "SET %s=%s\ndel $tmp$.bat\x1a", argv[1], argv[2]);
  142.         fclose(bfile);
  143.         while (kbhit())
  144.                 ;
  145.         KB_stuff("$tmp$\r");
  146. }
  147.